<?php
/**
 * @name 电子邮件表单
 * 
 * @author vipkwd <service@vipkwd.com>
 * @link https://github.com/wxy545812093/vipkwd-phpmailer
 * @link https://bootstrapmade.com/php-email-form/
 * @license http://www.apache.org/licenses/LICENSE-2.0
 * @copyright The PHP-Tools
 */

declare(strict_types=1);

namespace Vipkwd\PHPMailer;

use Vipkwd\PHPMailer\phpmailer\src\PHPMailer as GithubPHPMailer;
use Vipkwd\PHPMailer\common\PHPEmailForm;

class EmailForm
{

    private $contact;

    const ENCRYPTION_SMTPS = GithubPHPMailer::ENCRYPTION_SMTPS;
    const ENCRYPTION_STARTTLS = GithubPHPMailer::ENCRYPTION_STARTTLS;

    private function __construct()
    {
        $this->contact = new PHPEmailForm;
        // $this->ajaxVerify(false);
    }

    /**
     * 静态实例化
     */
    static function instance()
    {
        return new self;
    }

    /**
     * 收件人
     * 
     * @param string $email 收件人地址
     * @param string|null $emailError 收件人地址无效是的错误提示信息
     */
    public function to(string $email, ?string $emailError = null): self
    {
        $this->contact->to = $email;
        $emailError && $this->contact->invalid_to_email = $emailError;
        return $this;
    }

    /**
     * 设置邮件发送人
     * 
     * @param string $email 发送人地址
     * @param string $name 发送人显示名称
     * 
     * @param string|null $emailError 发送人地址错误时的提示信息
     * @param string|null $nameError 发送人显示名称错误时的提示信息
     * 
     */
    public function from(string $email, string $name, ?string $emailError = null, ?string $nameError = null): self
    {
        $this->contact->from_name = $name;
        $this->contact->from_email = $email;

        $emailError && $this->contact->invalid_from_email = $emailError;
        $nameError && $this->contact->invalid_from_name = $nameError;
        return $this;
    }

    /**
     * 邮件主题(标题)
     * 
     * @param string $subject 标题
     * @param string|null $subjectError 标题无效时的错误提示信息
     */
    public function subject(string $subject, ?string $subjectError = null): self
    {
        $this->contact->subject = $subject;
        $subjectError && $this->contact->invalid_subject = $subjectError;
        return $this;
    }

    /**
     * 指定邮件使用SMTP协议
     * 
     * @param string $username
     * @param string $password 账号密码(授权码)
     * @param string $host [126,163,aliyun,qq,或自定]
     * @param int $port
     * @param string $encryption SSL|TLS
     * 
     */
    public function smtp(string $username, string $password, string $host, int $port = 465, string $encryption = 'SSL'): self
    {
        switch (strtolower("" . $host)) {
            case "126":
                $host = 'smtp.126.com';
                $port = 465;
                $encryption = 'ssl';
                break;
            case "aliyun":
                $host = 'smtp.aliyun.com';
                $port = 465;
                $encryption = 'ssl';
                break;
            case "163":
                $host = 'smtp.163.com';
                $port = 465;
                $encryption = 'ssl';
                break;
            case "qq":
                $host = 'smtp.qq.com';
                $port = 465;
                $encryption = 'ssl';
                break;
        }

        $this->contact->smtp = [
            'host' => $host,
            'username' => $username,
            'password' => $password,
            'port' => $port,
            'encryption' => strtolower($encryption) == 'ssl' ? self::ENCRYPTION_SMTPS : self::ENCRYPTION_STARTTLS,
        ];
        return $this;
    }

    /**
     * 发送邮件正文
     * 
     * @param string $body 消息正文
     * @param int $lenVerify 长度校验码
     */
    public function send(string $body, int $lenVerify = 10): string
    {
        $this->addBody($body, $lenVerify);
        return $this->contact->send();
    }

    /**
     * 开启AJAX验证
     */
    public function ajaxVerify(bool $ajax = false): self
    {
        $this->contact->ajax = $ajax;
        $ajax && $this->contact->ajax_error = '抱歉，请求应该是 Ajax POST';
        return $this;
    }
    /**
     * 抄送
     * 
     * @param array $emails 抄送人地址列表 一维数组
     */
    public function cc(array $emails): self
    {
        $this->contact->cc = $emails;
        return $this;
    }

    /**
     * 秘送
     * 
     * @param array $emails 抄送人地址列表 一维数组
     */
    public function Bcc(array $emails): self
    {
        $this->contact->bcc = $emails;
        return $this;
    }
    /**
     * 添加附件
     * 
     * @param string $field 用于捕获$_FILES['xxxxxx']
     * @param int $maxMb 文件大小上限MB
     * @param array $exts 支持的文件扩展名 index-array
     */
    public function addAttachment(string $field, int $maxMb = 20, array $exts = ['jpeg', 'jpg', 'png', 'pdf', 'doc', 'docx']): self
    {
        $this->contact->add_attachment($field, $maxMb, $exts);
        return $this;
    }

    /**
     * 添加邮件正文
     * 
     * @param string $body 消息正文
     * @param int $lenVerify 长度校验码
     * 
     */
    private function addBody(string $body, int $lenVerify = 10)
    {
        $this->contact->add_message($this->contact->from_name, 'From');
        $this->contact->add_message($this->contact->from_email, 'Email');
        $this->contact->add_message($body, 'Message', $lenVerify <= 0 ? 4 : $lenVerify);
        $this->contact->short = '邮件正文内容长度不能少于' . ($lenVerify <= 0 ? 4 : $lenVerify) . '个字符';
    }
}